package de.mhus.lib.mandala.form;

import de.mhus.lib.mandala.Response;
import de.mhus.lib.mandala.attributes.BooleanAttribute;
import de.mhus.lib.mandala.layout.AbstractComposite;

public class Form extends AbstractComposite<FormInput,Object>{

	public enum VISUALISATION {
		BASIC,INLINE,HORIZONTAL;
		
		public String getStyleClass() {
			switch (this) {
			case BASIC: return "form";
			case INLINE: return "form-inline";
			case HORIZONTAL: return "form-horizontal";
			}
			return "";
		}
	};
	
	public enum MODE { READ, WRITE, READWRITE }
	
	
	private VISUALISATION visualisation = VISUALISATION.HORIZONTAL;
	private MODE mode = MODE.WRITE;
	private BooleanAttribute editableSwitch;
	
	public Form() {}
	
	public Form(FormInput ... inputs) {
		add(inputs);
	}
	
	public void add(FormInput ... inputs) {
		for (FormInput input : inputs)
			add(input, (Object)null);
	}
	
	@Override
	protected void doRender(Response out) {
				
		out.startTag("form", getVisualisation().getStyleClass(), getTagAttributes(), "role", "form");
		for (Container<FormInput, Object> item : this) {
			item.getItem().doRender(this, out);
		}
		out.endTag("form");
	}

	public VISUALISATION getVisualisation() {
		return visualisation;
	}

	public void setVisualisation(VISUALISATION visualisation) {
		this.visualisation = visualisation;
	}

	public MODE getMode() {
		return mode;
	}

	public void setMode(MODE mode) {
		this.mode = mode;
		if (getMode() == MODE.READWRITE && editableSwitch == null)
			editableSwitch = new BooleanAttribute();
	}

	public BooleanAttribute getEditableSwitch() {
		return editableSwitch;
	}

	public void setEditableSwitch(BooleanAttribute editableSwitch) {
		this.editableSwitch = editableSwitch;
	}


}
